package pin.db.schema.base;
import pin.db.schema.*;
import java.util.Vector;
import java.util.Collection;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.ResultSet;
public class ForumPostPeer extends BasePeer{
	public static final String tableName = "ForumPost";
	public static final String POSTID = "postId";
	public static final String TITLE = "title";
	public static final String POSTTIME = "postTime";
	public static final String CONTENT_S = "content_s";
	public static final String CLICKCOUNT = "clickCount";
	public static final String USERID = "userId";
	public static final String STATE = "state";
	public static final String REPLIES = "replies";
	public static final String CONTENT_R = "content_r";
	public static final String LASTMODIFY = "lastModify";
	public static final String AUTHOR = "author";
	public static final String CATEGORY = "category";
	public static final String LENGTH = "length";
	public static final String AUDITING = "auditing";
	public static final String TOP = "top";
	public static final String GROUPID = "groupId";
	public static final String ORDER = "order";
	public static final String[] columnNames=new String[]{"postId","title","postTime","content_s","clickCount","userId","state","replies","content_r","lastModify","author","category","length","auditing","top","groupId","order"};
	public static ForumPost getForumPostByPKs(Object postId, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(ForumPostPeer.tableName,ForumPostPeer.columnNames," TRUE  AND `postId`=? ",new Object[]{postId,},0, 1,conn);
			ForumPost forumPost = new ForumPost();
		fill(rs,forumPost);
		return forumPost;
	}
	public static ForumPost getForumPostReferenceColumnsOnlyByPKs(Object postId, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(ForumPostPeer.tableName,new String[]{"postId",}," TRUE AND `postId`=?",new Object[]{postId,}, 0,  1,conn);
			ForumPost forumPost = new ForumPost();
		fill(rs,forumPost);
		return forumPost;
	}
	public static ForumPost getForumPostByPostId(long postId, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(ForumPostPeer.tableName,ForumPostPeer.columnNames,"`postId`=?",new Object[]{postId},0, 1,conn);
			ForumPost forumPost = new ForumPost();
		fill(rs,forumPost);
		return forumPost;
	}
	public static ForumPost getForumPostByTitle(java.lang.String title, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(ForumPostPeer.tableName,ForumPostPeer.columnNames,"`title`=?",new Object[]{title},0, 1,conn);
			ForumPost forumPost = new ForumPost();
		fill(rs,forumPost);
		return forumPost;
	}
	public static ForumPost getForumPostByUserId(long userId, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(ForumPostPeer.tableName,ForumPostPeer.columnNames,"`userId`=?",new Object[]{userId},0, 1,conn);
			ForumPost forumPost = new ForumPost();
		fill(rs,forumPost);
		return forumPost;
	}
	public static ForumPost getForumPostByGroupId(long groupId, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(ForumPostPeer.tableName,ForumPostPeer.columnNames,"`groupId`=?",new Object[]{groupId},0, 1,conn);
			ForumPost forumPost = new ForumPost();
		fill(rs,forumPost);
		return forumPost;
	}
	public static void fill(ResultSet rs, ForumPost forumPost) throws SQLException {
		if(rs.next()){
			try{forumPost.setPostId(rs.getLong("postId"));}catch(Exception ex){}
			try{forumPost.setTitle(rs.getString("title"));}catch(Exception ex){}
			try{forumPost.setPostTime(rs.getTimestamp("postTime"));}catch(Exception ex){}
			try{forumPost.setContentS(rs.getString("content_s"));}catch(Exception ex){}
			try{forumPost.setClickCount(rs.getLong("clickCount"));}catch(Exception ex){}
			try{forumPost.setUserId(rs.getLong("userId"));}catch(Exception ex){}
			try{forumPost.setState(rs.getInt("state"));}catch(Exception ex){}
			try{forumPost.setReplies(rs.getLong("replies"));}catch(Exception ex){}
			try{forumPost.setContentR(rs.getString("content_r"));}catch(Exception ex){}
			try{forumPost.setLastModify(rs.getTimestamp("lastModify"));}catch(Exception ex){}
			try{forumPost.setAuthor(rs.getString("author"));}catch(Exception ex){}
			try{forumPost.setCategory(rs.getString("category"));}catch(Exception ex){}
			try{forumPost.setLength(rs.getString("length"));}catch(Exception ex){}
			try{forumPost.setAuditing(rs.getInt("auditing"));}catch(Exception ex){}
			try{forumPost.setTop(rs.getInt("top"));}catch(Exception ex){}
			try{forumPost.setGroupId(rs.getLong("groupId"));}catch(Exception ex){}
			try{forumPost.setOrder(rs.getTimestamp("order"));}catch(Exception ex){}
			forumPost.resetModified();
		}
	}
	public static void fill(ResultSet rs, Collection<ForumPost> forumPostCollection) throws SQLException {
		while(rs.next()){
			ForumPost forumPost=new ForumPost();
			try{forumPost.setPostId(rs.getLong("postId"));}catch(Exception ex){}
			try{forumPost.setTitle(rs.getString("title"));}catch(Exception ex){}
			try{forumPost.setPostTime(rs.getTimestamp("postTime"));}catch(Exception ex){}
			try{forumPost.setContentS(rs.getString("content_s"));}catch(Exception ex){}
			try{forumPost.setClickCount(rs.getLong("clickCount"));}catch(Exception ex){}
			try{forumPost.setUserId(rs.getLong("userId"));}catch(Exception ex){}
			try{forumPost.setState(rs.getInt("state"));}catch(Exception ex){}
			try{forumPost.setReplies(rs.getLong("replies"));}catch(Exception ex){}
			try{forumPost.setContentR(rs.getString("content_r"));}catch(Exception ex){}
			try{forumPost.setLastModify(rs.getTimestamp("lastModify"));}catch(Exception ex){}
			try{forumPost.setAuthor(rs.getString("author"));}catch(Exception ex){}
			try{forumPost.setCategory(rs.getString("category"));}catch(Exception ex){}
			try{forumPost.setLength(rs.getString("length"));}catch(Exception ex){}
			try{forumPost.setAuditing(rs.getInt("auditing"));}catch(Exception ex){}
			try{forumPost.setTop(rs.getInt("top"));}catch(Exception ex){}
			try{forumPost.setGroupId(rs.getLong("groupId"));}catch(Exception ex){}
			try{forumPost.setOrder(rs.getTimestamp("order"));}catch(Exception ex){}
			forumPost.resetModified();
			forumPostCollection.add(forumPost);
		}
	}
	public static boolean update(ForumPost forumPost, Connection conn) throws SQLException {
		Vector<String> columns = new Vector<String>();
		for (int i = 0; i < ForumPostPeer.columnNames.length; i++) {
			if (((ForumPostBase)forumPost).modified[i]) {
				columns.add(ForumPostPeer.columnNames[i]);
			}
		}
		if (columns.isEmpty()) return false;
		StringBuffer sql = new StringBuffer();
		for (String column : columns) {			sql.append(",`"+column+"` = ? ");		}
		sql.deleteCharAt(0);
		sql.insert(0, "UPDATE `ForumPost` SET ");
		sql.append("WHERE TRUE AND `postId` = ? ");
		PreparedStatement pstmt=conn.prepareStatement(sql.toString());
		for (int i=0;i<columns.size();i++) {
			String column = columns.elementAt(i);
			pstmt.setObject(i+1,forumPost.get(column));
		}
		pstmt.setObject(columns.size()+1+0,forumPost.get("postId"));
		int affected=pstmt.executeUpdate();
		return affected>0;
	}
	public static ResultSet insert(ForumPost forumPost, Connection conn) throws SQLException {
		StringBuffer sql1 = new StringBuffer();
		StringBuffer sql2 = new StringBuffer();
		for (String column : columnNames) {
			if (forumPost.get(column) != null) {
				sql1.append(",`" + column+"`");
				sql2.append(", ? ");
			}
		}
		sql1.deleteCharAt(0);
		sql2.deleteCharAt(0);
		String sql = "INSERT INTO `ForumPost` (" + sql1.toString() + ") VALUES (" + sql2.toString() + ") ";
		System.out.println(sql);
		PreparedStatement pstmt = conn.prepareStatement(sql);
		for (int i = 0, j = 0; i < columnNames.length; i++) {
			String column = columnNames[i];
			System.out.println(column);
			if (forumPost.get(column) != null) {
				pstmt.setObject(i + 1 + j, forumPost.get(column));
			}else{
				j--;
			}
		}
		int affected=pstmt.executeUpdate();
		return pstmt.getGeneratedKeys();
	}
	public static Collection<ForumPost> getForumPostCollection(String conditions, Object[] values, int offset, int limit, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(tableName,columnNames,conditions,values, offset,  limit,conn);
		Collection<ForumPost> collection=new Vector<ForumPost>();
		fill(rs,collection);
		return collection;
	}
}
